package com.ruoyi.fire.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.ResponseEntityInfo;
import com.ruoyi.common.enums.RequestMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.RequestService;
import com.ruoyi.fire.domain.ESuperviseLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fire.mapper.EUnitTagMapper;
import com.ruoyi.fire.domain.EUnitTag;
import com.ruoyi.fire.service.IEUnitTagService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 单位标签Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-30
 */
@Service
@RequiredArgsConstructor
public class EUnitTagServiceImpl extends ServiceImpl<EUnitTagMapper, EUnitTag> implements IEUnitTagService {
    @Autowired
    private EUnitTagMapper eUnitTagMapper;

    private final RequestService requestService;

    /**
     * 查询单位标签
     *
     * @param id 单位标签主键
     * @return 单位标签
     */
    @Override
    public EUnitTag selectEUnitTagById(Long id) {
        return eUnitTagMapper.selectEUnitTagById(id);
    }

    /**
     * 查询单位标签列表
     *
     * @param eUnitTag 单位标签
     * @return 单位标签
     */
    @Override
    public List<EUnitTag> selectEUnitTagList(EUnitTag eUnitTag) {
        return eUnitTagMapper.selectEUnitTagList(eUnitTag);
    }

    /**
     * 新增单位标签
     *
     * @param eUnitTag 单位标签
     * @return 结果
     */
    @Override
    public int insertEUnitTag(EUnitTag eUnitTag) {
        return eUnitTagMapper.insertEUnitTag(eUnitTag);
    }

    /**
     * 修改单位标签
     *
     * @param eUnitTag 单位标签
     * @return 结果
     */
    @Override
    public int updateEUnitTag(EUnitTag eUnitTag) {
        eUnitTag.setUpdateTime(DateUtils.getNowDate());
        return eUnitTagMapper.updateEUnitTag(eUnitTag);
    }

    /**
     * 批量删除单位标签
     *
     * @param ids 需要删除的单位标签主键
     * @return 结果
     */
    @Override
    public int deleteEUnitTagByIds(Long[] ids) {
        return eUnitTagMapper.deleteEUnitTagByIds(ids);
    }

    /**
     * 删除单位标签信息
     *
     * @param id 单位标签主键
     * @return 结果
     */
    @Override
    public int deleteEUnitTagById(Long id) {
        return eUnitTagMapper.deleteEUnitTagById(id);
    }

    /**
     * 数据同步
     *
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dataSync() {
        try {
            ResponseEntityInfo responseEntityInfo = requestService.advancedRequest(RequestMapper.UNIT_TAG, null);
            if (responseEntityInfo != null && responseEntityInfo.getErrno() == 0) {
                Object data = responseEntityInfo.getData();
                if (data == null) {
                    // 记录警告日志：返回数据为空
                    return;
                }

                // 使用条件删除替代全表删除，绕过保护机制
                getBaseMapper().delete(new LambdaQueryWrapper<EUnitTag>().isNotNull(EUnitTag::getId));
                List<EUnitTag> eUnitTags = JSON.parseArray(JSON.toJSONString(data), EUnitTag.class);
                if (eUnitTags != null && !eUnitTags.isEmpty()) {
                    // 设置更新时间
                    eUnitTags.forEach(item -> item.setUpdateTime(DateUtils.getNowDate()));
                    saveBatch(eUnitTags);
                }
            }
        } catch (Exception e) {
            // 记录错误日志
            throw new RuntimeException("数据同步过程中发生异常", e);
        }
    }
}
